package com.example.tellwin.mine.presenter;

import android.util.SparseArray;

import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.mine.bean.OrderContentBean;
import com.example.tellwin.mine.contract.OrderListContract;

import javax.inject.Inject;

/**
 * @author : LinZePeng
 * @date : 2020/12/4
 * @Describe ：
 **/
public class OrderListPresenter extends RxPresenter<OrderListContract.View> implements OrderListContract.Presenter<OrderListContract.View> {

    private TwjfApi mApi;

    public final int pageSize = 10;
    SparseArray<OrderTabBean> orderTabBeans = new SparseArray<>();

    @Inject
    public OrderListPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }

    public void clearOrderTab(){
        orderTabBeans.clear();
    }

    @Override
    public void getOrderList(int status) {
        if (orderTabBeans.get(status) == null) {
            orderTabBeans.put(status, new OrderTabBean(status, 0, 1, false));
        }else {//不为空代表已经加载过了，不在重复加载第一次
            return;
        }
        OrderTabBean orderTabBean = orderTabBeans.get(status);
        orderTabBean.pages = 0;
        orderTabBean.pageNum = 1;
        orderTabBean.isNotMore = false;
        String statusStr = null;//订单状态0待接单1已完成2已付款3已评价4已取消5不满意,不传或传null为全部
        if (status >= 0 && status <= 5) {
            statusStr = status + "";
        }
        mApi.getOrderList(statusStr, orderTabBean.pageNum + "", pageSize + "", new SimpleCallback<OrderContentBean>(mView) {
            @Override
            public void onSuccess(OrderContentBean contentBean) {
                orderTabBean.pageNum = contentBean.getPageNum();
                orderTabBean.pages = contentBean.getPages();
                if (contentBean.getOrderList() == null || contentBean.getOrderList().size() == 0) {
                    orderTabBean.isNotMore = true;
                    return;
                }
                mView.orderListResult(contentBean.getOrderList(), orderTabBean.status);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void loadMore(int status) {
        OrderTabBean orderTabBean = orderTabBeans.get(status);
        if (orderTabBean == null) {
            return;
        }
        if (orderTabBean.isNotMore) {
            return;
        }
        if (orderTabBean.pages != 0 && orderTabBean.pageNum == orderTabBean.pages) {
            orderTabBean.isNotMore = true;
            return;
        }
        orderTabBean.pageNum++;
        String statusStr = null;//订单状态0待接单1已完成2已付款3已评价4已取消5不满意,不传或传null为全部
        if (status >= 0 && status <= 5) {
            statusStr = status + "";
        }
        mApi.getOrderList(statusStr, orderTabBean.pageNum + "", pageSize + "", new SimpleCallback<OrderContentBean>(mView) {
            @Override
            public void onSuccess(OrderContentBean contentBean) {
                orderTabBean.pageNum = contentBean.getPageNum();
                orderTabBean.pages = contentBean.getPages();
                if (contentBean.getOrderList() == null || contentBean.getOrderList().size() == 0) {
                    orderTabBean.isNotMore = true;
                    return;
                }
                mView.loadMoreResult(contentBean.getOrderList(), status);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    class OrderTabBean {
        public int status;
        public int pages;
        public int pageNum;
        public boolean isNotMore = true;

        public OrderTabBean(int status, int pages, int pageNum, boolean isNotMore) {
            this.status = status;
            this.pages = pages;
            this.pageNum = pageNum;
            this.isNotMore = isNotMore;
        }
    }
}
